---
id: extract-markdown-code-blocks
title: Extract Markdown Code Blocks Node
sidebar_label: Extract Markdown Code Blocks
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

![Extract Markdown Code Blocks Node Screenshot](./assets/extract-markdown-code-blocks-node.png)

## Overview

The Extract Markdown Code Blocks Node is used to extract code blocks from a Markdown text. It extracts all code blocks and the specified languages for each block. The node outputs the first matched block, all matched blocks, and the languages specified for the blocks.

A markdown code block is defined as a code block that is surrounded by three backticks on each side. For example:

````markdown
Here is some JavaScript code:

```javascript
console.log('Hello, world!');
```
````

This node is useful when working with LLMs that have been trained extensively on replying with markdown data.

<Tabs
  defaultValue="inputs"
  values={[
    {label: 'Inputs', value: 'inputs'},
    {label: 'Outputs', value: 'outputs'},
    {label: 'Editor Settings', value: 'settings'},
  ]
}>

<TabItem value="inputs">

## Inputs

| Title | Data Type | Description                                         | Default Value | Notes                                                         |
| ----- | --------- | --------------------------------------------------- | ------------- | ------------------------------------------------------------- |
| Input | `string`  | The Markdown text from which to extract code blocks | (required)    | The input will be coerced into a string if it is not a string |

</TabItem>

<TabItem value="outputs">

## Outputs

| Title       | Data Type  | Description                                                                                      | Notes                                                                                                        |
| ----------- | ---------- | ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------ |
| First Block | `string`   | The first code block found in the input Markdown text                                            | If no code block is found, this output will not be ran                                                       |
| All Blocks  | `string[]` | All code blocks found in the input Markdown text                                                 | If no code block is found, this output will be an empty array                                                |
| Languages   | `string[]` | The languages specified for each code block in the input Markdown text, in the order they appear | If no language is specified for a code block, the corresponding element in the array will be an empty string |

</TabItem>

<TabItem value="settings">

## Editor Settings

This node has no configurable editor settings.

</TabItem>

</Tabs>

## Example 1: Extract code blocks from a Markdown text

1. Create a [Text Node](./text.mdx) and set the text to the following Markdown text:

   ````markdown
   Here is some JavaScript code:

   ```javascript
   console.log('Hello, world!');
   ```

   And here is some Python code:

   ```python
   print('Hello, world!')
   ```

   That's it!
   ````

2. Create an Extract Markdown Code Blocks Node and connect the Text Node to its `Input` input.
3. Run the graph. The `First Block` output of the Extract Markdown Code Blocks Node should be `console.log('Hello, world!');`, the `All Blocks` output should be an array containing `console.log('Hello, world!');` and `print('Hello, world!')`, and the `Languages` output should be an array containing `javascript` and `python`.

![Extract Markdown Code Blocks Node Example 1](./assets/extract-markdown-code-blocks-node-example-01.png)

## Error Handling

The Extract Markdown Code Blocks Node will not error under normal circumstances. If the input text does not contain any code blocks, the `First Block` output will not be ran, and the `All Blocks` and `Languages` outputs will be empty arrays.

## FAQ

**Q: What happens if a code block does not specify a language?**

A: The corresponding element in the `Languages` output array will be an empty string.

**Q: What happens if the input text contains non-Markdown text?**

A: The node will ignore any non-Markdown text and only extract code blocks. If the input text does not contain any code blocks, the `First Block` output will not be ran, and the `All Blocks` and `Languages` outputs will be empty arrays.

## See Also

- [Extract JSON Node](./extract-json.mdx)
- [Extract YAML Node](./extract-yaml.mdx)
- [Extract Object Path Node](./extract-object-path.mdx)
- [Extract with Regex Node](./extract-with-regex.mdx)
- [Text Node](./text.mdx)
- [Split Text Node](./split-text.mdx)

```

```
